//
//  FlowDetail.swift
//  Employee
//
//  Created by swordjoy on 2017/6/27.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct FlowDetail {
  let formItems: [FormItem]
  let checkPeoples: [CheckPeople]
  let flowType: String
  
  init(json: JSON) {
    self.formItems = FormItem.array(from: json["widgetDtos"].arrayValue)
    self.flowType = json["flowType"].stringValue
    
    if "ROLE" == self.flowType {
        let tempArray = CheckPeople.array(from: json["flowSortDtos"].arrayValue, isStaff: false)
        self.checkPeoples = tempArray.filter({ (people) -> Bool in
            if people.roles.isEmpty {
                return false
            }
            return true
        })
        
    }
    else if "STAFF" == self.flowType {
        self.checkPeoples = CheckPeople.array(from: json["flowSortDtos"].arrayValue, isStaff: true)
    }
    else {
        self.checkPeoples = []
    }
  }
  
}

struct CheckPeople {
  let approvalName: String
  let approvalStaffAvatar: String
  let approvalStaffId: String
  let roles: [CheckRole]
  
    init(json: JSON, isStaff: Bool) {
    if isStaff {
        self.roles = []
      self.approvalName = json["approvalName"].stringValue
      self.approvalStaffAvatar = json["approvalStaffAvatar"].stringValue
      self.approvalStaffId = json["approvalStaffId"].stringValue
    }
    else {
      self.roles = CheckRole.array(from: json["staffToRoleDtos"].arrayValue)
      self.approvalName = roles.first?.roleStaffName ?? ""
      self.approvalStaffAvatar = roles.first?.roleStaffAvatar ?? ""
      self.approvalStaffId = roles.first?.roleStaffId ?? ""
    }
  }
  
    static func array(from jsonArray: [JSON], isStaff: Bool) -> [CheckPeople] {
    return jsonArray.map({
        return CheckPeople(json: $0, isStaff: isStaff)
    })
  }
  
}

struct CheckRole: ModelProtocol {
  let roleStaffName: String
  let roleStaffAvatar: String
  let roleStaffId: String
  
  init(json: JSON) {
    self.roleStaffName = json["roleStaffName"].stringValue
    self.roleStaffAvatar = json["roleStaffAvatar"].stringValue
    self.roleStaffId = json["roleStaffId"].stringValue
  }
  
  static func array(from jsonArray: [JSON]) -> [CheckRole] {
    return jsonArray.map({ return CheckRole(json: $0) })
  }
}

